<template>
  <el-dialog
    class="thematic-setting-comp"
    :title="title"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    :visible="visible"
    :width="$setRem(950)"
    :before-close="handleCancel">
    <el-form
      :model="ruleForm"
      :rules="rules"
      label-suffix="："
      size="medium"
      ref="ruleForm"
      :label-width="$setRem(140)"
      class="rule-form"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="专题名称" prop="materialName" required>
            <el-input
              type="text"
              placeholder="请输入材料名称"
              v-model="ruleForm.materialName">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位" prop="unit" required>
            <el-input
              type="text"
              placeholder="请输入单位"
              v-model="ruleForm.unit">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'add'
    },
    options: {
      type: Object,
      default: () => ({})
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        id: '',
        materialName: '',
        unit: '',
        remark: ''
      },
      rules: {
        materialName: [
          { required: true, message: '请输入材料名称' },
        ],
        unit: [
          { required: true, message: '请输入单位' },
        ],
      },
    };
  },
  computed: {
    title() {
      return `${this.type === 'add' ? '新增' : '编辑'}专题`;
    }
  },
  watch: {
    visible(val) {
      if (!val) {
        Object.keys(this.ruleForm).forEach(key => {
          this.ruleForm[key] = '';
        });
      }
    },
    options: {
      deep: true,
      handler(data) {
        const keys = Object.keys(this.ruleForm);
        data && Object.keys(data).length && keys.forEach(key => {
          this.ruleForm[key] = data[key];
        });
      }
    }
  },
  created() {},
  methods: {
    handleCancel() {
      this.$emit('cancel', false);
    }
  },
};
</script>

<style lang="scss" scoped>
  @import '../../../assets/styles/dialog';
</style>
